package com.music.service.impl;

import com.laoxuai.beans.annotation.Autowired;
import com.laoxuai.beans.annotation.Service;
import com.laoxuai.ibatis.util.Example;
import com.laoxuai.ibatis.util.PageInfo;
import com.laoxuai.util.DateUtils;
import com.laoxuai.util.StringUtils;
import com.music.dao.ArtistDao;
import com.music.pojo.Artist;
import com.music.pojo.Tag;
import com.music.service.ArtistService;

import java.sql.Date;
import java.sql.SQLException;
import java.util.List;

@Service
public class ArtistServiceImpl implements ArtistService {

    @Autowired
    private ArtistDao artistDao;

    @Override
    public int insert(Artist artist) {
        System.out.println(artist.getName());
        if(artist.getName()==null){
            return -1;//当插入的歌手姓名为空时，不允许插入。
        }

        Integer newID;
        try {
            Integer maxID=artistDao.selectMaxId();
            if(maxID==null){
                newID=1;
            }else {
                newID = maxID + 1;
            }
            artist.setId(newID);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


        long time = System.currentTimeMillis();
        Date date=new Date(time);
        artist.setCreateTime(date);
        artist.setIsDeleted(1);


        try {
            return artistDao.insert(artist);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;//当返回值为0时代表程序正常运行已完成插入
    }

    @Override
    public int delete(Integer id) {
        try {
            Artist reartist=artistDao.getByPrimaryKey(id);
            if(reartist==null){
                return -1;//当id传入的不存在时返回-1
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        try {
            return artistDao.delete(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Artist artist) {
        Integer id=artist.getId();
        try {
            Artist reartist=artistDao.getByPrimaryKey(id);//当歌手不存在时返回-1
            if(reartist==null){
                return -1;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            return artistDao.update(artist);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Artist getByPrimaryKey(Integer id) {
        try {
            return artistDao.getByPrimaryKey(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Artist> listPage(Artist artist, int page, int size) {
        artist.setIsDeleted(0);
        try {
            return artistDao.listPage(artist, page, size);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<Artist> listPageInfo(Artist artist, int page, int size) {
        try {
            return artistDao.listPageInfo(artist, page, size);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    public List<Artist> selectAll() {
        try {
            return artistDao.selectAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
    @Override
    public List<Artist> selectByExample(Artist artist) {
        Example example = createExmaple(artist);
        try {
            return artistDao.selectByExample(example);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    private Example createExmaple(Artist artist) {
        Example example = new Example(Artist.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO
        if(artist != null){
            if(!StringUtils.isEmpty(artist.getId())){
                criteria.andEqualTo("id", artist.getId());
            }
            if(!StringUtils.isEmpty(artist.getName())){
                criteria.andLike("name", "%"+artist.getName()+"%");
            }
            if(!StringUtils.isEmpty(artist.getPicUrl())){
                criteria.andEqualTo("pictureUrl", artist.getPicUrl());
            }
            if(!StringUtils.isEmpty(artist.getBriefDesc())){
                criteria.andEqualTo("describe", artist.getBriefDesc());
            }
            if(!StringUtils.isEmpty(artist.getArtistOrder())){
                criteria.andEqualTo("artistOrder", artist.getArtistOrder());
            }
            if(!StringUtils.isEmpty(artist.getMusicSize())){
                criteria.andEqualTo("musicSize", artist.getMusicSize());
            }
            if(!StringUtils.isEmpty(artist.getCreateTime())){
                criteria.andEqualTo("createTime", artist.getCreateTime());
            }
            if(!StringUtils.isEmpty(artist.getUpdateTime())){
                criteria.andEqualTo("updateTime", artist.getUpdateTime());
            }
            if(!StringUtils.isEmpty(artist.getIsDeleted())){
                criteria.andEqualTo("isDeleted", artist.getIsDeleted());
            }
        }
        return example;
    }

    /**
     * Artist 构建查询对象
     * @param artist
     * @return
     */
    public Example createExample(Artist artist){
        Example example = new Example(Artist.class);
        Example.Criteria criteria = example.createCriteria();
        if(artist != null){
            if(!StringUtils.isEmpty(artist.getId())){
                criteria.andEqualTo("id", artist.getId());
            }
            if(!StringUtils.isEmpty(artist.getName())){
                criteria.andLike("name", "%"+artist.getName()+"%");
            }
            if(!StringUtils.isEmpty(artist.getPicUrl())){
                criteria.andEqualTo("pictureUrl", artist.getPicUrl());
            }
            if(!StringUtils.isEmpty(artist.getBriefDesc())){
                criteria.andEqualTo("describe", artist.getBriefDesc());
            }
            if(!StringUtils.isEmpty(artist.getArtistOrder())){
                criteria.andEqualTo("artistOrder", artist.getArtistOrder());
            }
            if(!StringUtils.isEmpty(artist.getMusicSize())){
                criteria.andEqualTo("musicSize", artist.getMusicSize());
            }
            if(!StringUtils.isEmpty(artist.getCreateTime())){
                criteria.andEqualTo("createTime", artist.getCreateTime());
            }
            if(!StringUtils.isEmpty(artist.getUpdateTime())){
                criteria.andEqualTo("updateTime", artist.getUpdateTime());
            }
            if(!StringUtils.isEmpty(artist.getIsDeleted())){
                criteria.andEqualTo("isDeleted", artist.getIsDeleted());
            }
        }
        return example;
    }


}
